package com.geekchique.runningbuddy.controller;

import java.util.Date;
import java.util.TreeMap;

import android.content.Context;
import android.location.Location;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.geekchique.runningbuddy.activity.StopClock;
import com.geekchique.runningbuddy.activity.stopclock.StopClockThread;
import com.geekchique.runningbuddy.activity.stopclock.StopClockTools;
import com.geekchique.runningbuddy.model.Jog;
import com.geekchique.runningbuddy.provider.ActivityID;
import com.geekchique.runningbuddy.util.DBHelperImpl;
import com.geekchique.runningbuddy.view.StopClockTargetView;
import com.geekchique.runningbuddy.view.StopClockView;

public class StopClockControllerImpl implements IStopClockController{

    protected StopClockThread timerRunnerThread;
	private StopClockView stopClockView;
	StopClockTargetView stopClockTargetView;
	private StopClock stopClockActivity;
    private DBHelperImpl dbHelper;


    public StopClockControllerImpl(StopClock stopClockActivity){
    	this.stopClockActivity = stopClockActivity;
    	dbHelper = new DBHelperImpl(stopClockActivity.getContext());
    }


	public void createStopClockView(Context activity, TextSwitcher stopClockTextSwitcher, Button startButton) {
			stopClockView = new StopClockView(
				activity,
				stopClockTextSwitcher,
				startButton
			);

		stopClockView.setStartStopButtonClickListener(executeAStart);
		stopClockView.resetStopClock();

	}

	public void createStopClockTargetView(Context context,
			CheckBox checkBox,
			TextView hours,
			TextView minutes,
			TextView seconds,
			TextView milliseconds,
			Button hoursUp,
			Button hoursDown,
			TextView hoursSep,
			Button minutesUp,
			Button minutesDown,
			TextView minutesSep,
			Button secsUp,
			Button secsDown,
			TextView secondsSep,
			Button millisecsUp,
			Button millisecsDown) {

	        timerRunnerThread = new StopClockThread(stopClockServiceHandler);

			stopClockTargetView = new StopClockTargetView(
					context,
					checkBox,
					hours,
					minutes,
					seconds,
					milliseconds,
					hoursUp,
					hoursDown,
					hoursSep,
					minutesUp,
					minutesDown,
					minutesSep,
					secsUp,
					secsDown,
					secondsSep,
					millisecsUp,
					millisecsDown,
					timerRunnerThread
			);

			stopClockTargetView.hideSetTargetTime();
	}


	protected Handler stopClockServiceHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ActivityID.UPDATESTOPCLOCK:
				updateRunningStopClock();
				break;
			}
			super.handleMessage(msg);
		}
	};


	public void start() {
		timerRunnerThread.start();
		stopClockActivity.startStopClockService();
		stopClockView.switchStopClockButton(ActivityID.STOPCLOCK_RUNNING);
		stopClockView.setStartStopButtonClickListener(executeAStop);
	}

	public void stop() {
		timerRunnerThread.stop();
		stopClockActivity.stopStopClockService();
		saveJogLogEntry("jog" + new Date().getTime(), timerRunnerThread.getStopClock().getStartTime(), timerRunnerThread.getStopClock().getStopTime());
		stopClockView.switchStopClockButton(ActivityID.STOPCLOCK_NOT_RUNNING);
		stopClockView.setStartStopButtonClickListener(executeAStart);
	}

	private void updateRunningStopClock() {
		String elapsedTime = timerRunnerThread.getElapsedTime();

		if (timerRunnerThread.targetIsSet() && isTargetTimeReached(elapsedTime)) {
			stop();
			stopClockView.setStopClock(timerRunnerThread.getTargetTime());
		} else {
			stopClockView.setStopClock(elapsedTime);
		}
	}

	private boolean isTargetTimeReached(String elapsedTime) {
		int stopClock = StopClockTools.compare(timerRunnerThread.getTargetTime(), elapsedTime);

		return stopClock == StopClockTools.EQUAL	|| stopClock == StopClockTools.GREATER_THAN;
	}


	private OnClickListener executeAStart = new OnClickListener() {
		public void onClick(View v) {
			start();
		}
	};
	private OnClickListener executeAStop = new OnClickListener() {
		public void onClick(View v) {
			stop();
		}
	};


	protected void saveJogLogEntry(String name, long startTime, long stopTime) {

		TreeMap<Long, Location> locationsAndTimes = new TreeMap<Long, Location>();

		addJogTime(startTime, -4229920, 55846809, locationsAndTimes);
    	addJogTime(startTime, -4231200, 55846809, locationsAndTimes);
    	addJogTime(stopTime, -4236400, 55851200, locationsAndTimes);
    	addJogTime(stopTime, -4236800, 55851000, locationsAndTimes);


		dbHelper.addJogEntry(new Jog(name, name, locationsAndTimes));
//		dbHelper.addJogEntry(new Jog(name, startTime, stopTime));
	}

	private void addJogTime(long time, int longitude, int latitude, TreeMap<Long, Location> listOfJogTimes) {
		Location pointLocation;
		pointLocation = new Location();
    	pointLocation.setLongitude(longitude);
    	pointLocation.setLatitude(latitude);
    	listOfJogTimes.put(time, pointLocation);
	}


}
